package com.lxw.station.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lxw.station.dto.GitProjectDTO;
import com.lxw.station.entity.GitProject;
import com.lxw.station.service.GitProjectService;
import com.lxw.station.service.RestTemplateService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
public class GitController {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private GitProjectService gitProjectService;

    @SneakyThrows
    @GetMapping("/gitProjectDTOList")
    public void gitProjectDTOList() {
        String curl = "curl 'https://api.indexea.com/v1/search/widget/wjawvtmm7r5t25ms1u3d?query=1048&q=%E6%AF%95%E4%B8%9A&from=0&size=20&sort_by_f=' \\\n" +
                "  -H 'Accept-Language: zh-CN,zh;q=0.9' \\\n" +
                "  -H 'Connection: keep-alive' \\\n" +
                "  -H 'Origin: https://so.gitee.com' \\\n" +
                "  -H 'Referer: https://so.gitee.com/?q=%E6%AF%95%E4%B8%9A' \\\n" +
                "  -H 'Sec-Fetch-Dest: empty' \\\n" +
                "  -H 'Sec-Fetch-Mode: cors' \\\n" +
                "  -H 'Sec-Fetch-Site: cross-site' \\\n" +
                "  -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.289 Safari/537.36' \\\n" +
                "  -H 'accept: application/json' \\\n" +
                "  -H 'sec-ch-ua: \"Not.A/Brand\";v=\"8\", \"Chromium\";v=\"114\", \"Google Chrome\";v=\"114\"' \\\n" +
                "  -H 'sec-ch-ua-mobile: ?0' \\\n" +
                "  -H 'sec-ch-ua-platform: \"Windows\"' \\\n" +
                "  -H 'userid: df0be153-eab1-44be-9569-2bccaeff628b' \\\n" +
                "  -H 'x-token;' \\\n" +
                "  --compressed";
        //              https://api.indexea.com/v1/search/widget/wjawvtmm7r5t25ms1u3d?query=1048&q=%E8%AF%BE%E7%A8%8B%E8%AE%BE%E8%AE%A1&from=0&size=20&sort_by_f=
        String urlMb = "https://api.indexea.com/v1/search/widget/wjawvtmm7r5t25ms1u3d?query=1048&q=${q}&from=${from}&size=100&sort_by_f=Star 数";

        int size = 100;
        int page = 1;
        int total = 0;
        String q = "毕业论文";
        q = "毕业项目";
        q = "期末作业";
        q = "springboot";
        int from;
        List<GitProjectDTO> projectDTOS = new ArrayList<>();
        do {
            from = (page - 1) * size;
            page++;
            try {
                String url = urlMb.replace("${q}", q).replace("${from}", String.valueOf(from));
                System.out.println("url = " + url);
                String exchange = RestTemplateService.exchange(url, HttpMethod.GET);
                total = JSON.parseObject(exchange).getJSONObject("hits").getJSONObject("total").getIntValue("value");
                JSONArray hits = JSON.parseObject(exchange).getJSONObject("hits").getJSONArray("hits");
                for (int i = 0; i < hits.size(); i++) {
                    JSONObject fields = hits.getJSONObject(i).getJSONObject("fields");
                    String id = fields.getJSONArray("id").toJavaList(String.class).get(0);
                    String title = fields.getJSONArray("title").toJavaList(String.class).get(0);
                    title = title.substring(title.indexOf("/") + 1);
                    String description = fields.getJSONArray("description").toJavaList(String.class).get(0);
                    String cloneUrl = fields.getJSONArray("url").toJavaList(String.class).get(0);
                    GitProjectDTO projectDTO = new GitProjectDTO(id, title, description, cloneUrl);
                    log.info(JSON.toJSONString(projectDTO));
                    projectDTOS.add(projectDTO);
                }
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } while ((from + size) < total);
        log.info(JSON.toJSONString(projectDTOS));
        redisTemplate.opsForValue().set("gitee:" + q, projectDTOS);
    }

    public void init() {
        List<GitProjectDTO> gitProjectDTOS = (List<GitProjectDTO>) redisTemplate.opsForValue().get("gitee:毕业论文");
        for (GitProjectDTO gitProjectDTO : gitProjectDTOS) {
            try {
                GitProject gitProject = new GitProject();
                BeanUtils.copyProperties(gitProjectDTO, gitProject);
                gitProjectService.save(gitProject);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

//        redisTemplate.opsForHash().putAll("gitee:project",map);
    }


}
